#pragma once

#include "kernel/agv/i_agv_status_provider.h"
#include "kernel/interfaces/ILogger.h"
#include <QTimer>
#include <memory>

namespace application {
namespace simulation {

class SimulationEngine;

/**
 * @brief 仿真AGV状态提供者
 * @details 实现 IAgvStatusProvider 接口，定期从物理模拟器获取状态并推送
 */
class SimStatusProvider : public QObject, public kernel::agv::IAgvStatusProvider {
    Q_OBJECT

public:
    explicit SimStatusProvider(
        int agvNumber,
        SimulationEngine* engine,
        std::shared_ptr<kernel::ILogger> logger,
        QObject* parent = nullptr
    );

    ~SimStatusProvider() override = default;

    // ==================== IAgvStatusProvider 接口实现 ====================

    kernel::Result<void> start() override;

    void stop() override;

    bool isRunning() const override;

    void setStatusUpdateCallback(StatusUpdateCallback callback) override;

    int getAgvNumber() const override { return m_agvNumber; }

    /**
     * @brief 手动触发状态更新（由仿真引擎调用）
     */
    void updateStatus();

    /**
     * @brief 设置更新频率
     * @param intervalMs 更新间隔（毫秒）
     */
    void setUpdateInterval(int intervalMs);

private slots:
    void onTimerTimeout();

private:
    int m_agvNumber;
    SimulationEngine* m_engine;
    std::shared_ptr<kernel::ILogger> m_logger;
    StatusUpdateCallback m_callback;

    QTimer* m_timer;
    bool m_running;
    int m_updateIntervalMs;

    static constexpr int DEFAULT_UPDATE_INTERVAL_MS = 200;  // 5 Hz
};

} // namespace simulation
} // namespace application
